﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Google.GData.Calendar;
using Google.GData.Extensions;
using Google.GData.Client;



namespace CalenLoic
{
    class CalendarManager
    {
        private string uri;
        private CalendarService servicio;
        private string[][] attacks;
        public CalendarManager(string calendarURI)
        {
            servicio = new CalendarService("CalenLOIC"); // Probablemente lo mejor sea no cambiar
            uri = calendarURI;
            attacks = new string[0][];
        }
        private string[] parseEvents(string cadena) {
            string[] datos = cadena.Split(' ');
            if (datos.Length != 3 && datos[0] != "a:")
            {
                MessageBox.Show(datos.ToString());
                return null;
            }
            return datos;
        }
        private void addAttacks(string[] attack)
        {
            int indexAndSize = attacks.Length + 1;
            Array.Resize(ref attacks, indexAndSize);
            attacks[indexAndSize - 1] = attack;
        }
        private EventFeed getEventsFeed()
        {
            EventQuery consulta = new EventQuery();
            consulta.Uri = new Uri(uri);
            consulta.StartTime = DateTime.Now;
            EventFeed eventos = servicio.Query(consulta);
            return eventos;
        }
        public void refresh()
        {

            EventFeed eventos = getEventsFeed();
            foreach (EventEntry evento in eventos.Entries)
            {
                string titulo = evento.Title.Text.ToString();
                string[] eventParsed = parseEvents(titulo);
                if (eventParsed != null)
                {
                    addAttacks(eventParsed);
                }
            }
        }
        public void showAttacks() // Just a debugging function
        {
            foreach (string[] data in attacks)
            {
                string host = data[1];
                string puerto = data[2];
                MessageBox.Show("Attack " + host + " at port " + puerto);
            }
        }
    }
}
